import React, { Component } from "react";
import MainLayout from "@component/mainLayout/mainLayout";
import { Toast } from "antd-mobile";
import { connect } from "react-redux";
import { InterfaceG } from "@config/Config";
import BackModal from "@component/backModal/index";
import XhfTwoInfo from "@views/viewCommon/xhfTwoInfo/index";
import yhTools from "@tools/yh";
import { checkBonus } from "@redux/Action/bank/bank";

/**
 * 606 二要素返显
 */
@connect(
	state => ({
		applyNo: state.getAppPlatformsDatas.applyNo,
		lnid: state.getAppPlatformsDatas.lnid,
		devicesInfo: state.getAppPlatformsDatas.devicesInfo
	}),
	dispatch => ({
		checkBonus: (val1, val2) => dispatch(checkBonus(val1, val2))
	})
)
class certification extends Component {
	constructor(props) {
		super(props);
		this.state = {
			idCard: "",
			userName: "",
			isVisible: false,
			isBackModalVisible: false,
			headerBanner: "",
			inputDisabled: true
		};
	}

	componentDidMount = () => {
		this.getBannerList();
	};
	/**科罗纳调授信接口 */
	xhfAppSubApply = () => {
		const { lnid, applyNo } = this.props;
		let objs = {
			url: InterfaceG.submitCredit,
			params: {
				lnid: lnid,
				applyNo: applyNo,
				userDeviceInfo: JSON.stringify(this.props.devicesInfo),
				productNo: "601",
				version: "1.0"
			}
		};
		Toast.loading("加载中...", 100);
		yhTools.request(objs).then(res => {
			Toast.hide();
			if (res.code === "0") {
				const status = res.data.status;
				if (status == "1") {
					this.props.history.push({
						pathname: "/xhfHomeCredit"
					});
				} else if (status == "4") {
					this.props.history.push({
						pathname: "/xhfOpenFail",
						state: { message: res.data.message }
					});
				} else if (status == "2" || status == "3") {
					//授信中
					//科罗纳去10秒倒计时页面
					this.props.history.push("/xhfLivenessRet");
				}
			} else {
				Toast.info(res.message, 2);
			}
		});
	};
	/**返回显示 二要素 */
	getTwoElementInfo() {
		const objs = {
			url: InterfaceG.queryTwoElementInfo,
			params: {}
		};
		Toast.loading("加载中...", 100);
		yhTools.request(objs).then(res => {
			if (res.code === "0") {
				const { data = {} } = res;
				if (data.userName && data.idNo) {
					this.setState({
						userName: data.userName,
						idCard: data.idNo
					});
				}
				Toast.hide();
			} else {
				Toast.info(res.message, 2);
			}
			//获取606二要素奖励
			this.props.checkBonus("60601");
		});
	}
	saveIdCrad = () => {
		this.xhfAppSubApply();
	};

	/**获取实名认证头部banner */
	getBannerList() {
		const params = {
			url: InterfaceG.getChannelBannerList,
			params: {
				pageType: "xhf_auth_image",
				productNo: "601"
			},
			_alertShow: false
		};
		yhTools.request(params).then(res => {
			const { code, data = {} } = res;
			if (code == "0") {
				this.setState({
					headerBanner:
						data["1"] && data["1"].length > 0 ? data["1"]["0"]["imgUrl"] : ""
				});
			}
			this.getTwoElementInfo();
		});
	}
	onModalClick = booleanValue => {
		this.setState({
			isBackModalVisible: false
		});
		!booleanValue && this.props.history.push("/xhfHomeNoCredit");
	};
	showModal = () => {
		this.setState({
			isVisible: false,
			isBackModalVisible: true
		});
	};
	render() {
		const {
			isBackModalVisible,
			headerBanner,
			userName,
			idCard,
			inputDisabled
		} = this.state;
		const params = {
			headerBanner,
			userName,
			idCard,
			inputDisabled,
			history: this.props.history
		};
		const { lnid } = this.props;
		const _pageParams = {
			header: {
				title: "实名认证"
			},
			componentDidRecover: () => {
				this.getBannerList();
			},
			cacheLifecycles: this.props.cacheLifecycles,
			isModal: true,
			history: this.props.history
		};
		return (
			<MainLayout pageParams={_pageParams} handleBack={this.showModal}>
				{/**挽留页 */}
				<BackModal
					product={lnid}
					isVisible={isBackModalVisible}
					onClick={this.onModalClick}
				/>
				{/**二要素回显 */}
				<XhfTwoInfo pageParams={params} Submit={this.saveIdCrad}></XhfTwoInfo>
			</MainLayout>
		);
	}
}
export default certification;
